package cn.globalph.housekeeper.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import cn.globalph.housekeeper.R;


public class NiceImageVIew extends AppCompatImageView {
    public static final String TAG = "NiceImageVIew";

    private PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    private RectF srcRectF;
    Path path;
    Path srcPath;
    Paint paint;
    float[] srcRadii;
    int leftTopRadio;
    int rightTopRaido;
    int leftBottomRadio;
    int rightBottomRadio;
    float borderWidth;
    int width = 0;
    int height = 0;
    boolean isCircle = false;
    float radius = 0;

    public NiceImageVIew(Context context) {
        super(context);
        init();
    }

    public NiceImageVIew(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NiceImageVIew);
        leftTopRadio = ta.getInteger(R.styleable.NiceImageVIew_left_top_radius,-1);
        rightTopRaido = ta.getInteger(R.styleable.NiceImageVIew_right_top_radius,-1);
        leftBottomRadio = ta.getInteger(R.styleable.NiceImageVIew_left_bottom_radius,-1);
        rightBottomRadio = ta.getInteger(R.styleable.NiceImageVIew_right_bottom_radius,-1);
        borderWidth = ta.getFloat(R.styleable.NiceImageVIew_border_width,0f);
        isCircle = ta.getBoolean(R.styleable.NiceImageVIew_is_circle,false);

        if(leftTopRadio == -1 && rightTopRaido == -1 && leftBottomRadio == -1 && rightBottomRadio == -1){
            srcRadii[0] = srcRadii[1] = dp2px(8);
            srcRadii[2] = srcRadii[3] = dp2px(8);
            srcRadii[4] = srcRadii[5] = dp2px(8);
            srcRadii[6] = srcRadii[7] = dp2px(8);
        }else{
            srcRadii[0] = srcRadii[1] = leftTopRadio == -1?0:dp2px(leftTopRadio);
            srcRadii[2] = srcRadii[3] = rightTopRaido == -1?0:dp2px(rightTopRaido);
            srcRadii[4] = srcRadii[5] = leftBottomRadio == -1?0:dp2px(leftBottomRadio);
            srcRadii[6] = srcRadii[7] = rightBottomRadio == -1?0:dp2px(rightBottomRadio);
        }
    }

    public void init(){
        srcRadii = new float[]{0,0,0,0,0,0,0,0};
        path = new Path();
        paint = new Paint();

        if (Build.VERSION.SDK_INT <= 27) {
            xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        } else {
            xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
            srcPath = new Path();
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = right-left;
        height = bottom - top;
        radius = Math.min(width,height)/2;
        srcRectF = new RectF(0,0,width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(width == 0 || height == 0){
            return;
        }
        // 使用离屏缓存，新建一个srcRectF区域大小的图层
        canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
        // ImageView自身的绘制流程，即绘制图片
        super.onDraw(canvas);
        paint.reset();
        path.reset();
        if(srcPath != null){
            srcPath.reset();
        }
        // 给path添加一个圆角矩形或者圆形
        if (isCircle) {
            path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);
        } else {
            path.addRoundRect(srcRectF, srcRadii, Path.Direction.CCW);
        }
        paint.setAntiAlias(true);
        // 画笔为填充模式
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        // 设置混合模式
        paint.setXfermode(xfermode);
        // 绘制path
        if (Build.VERSION.SDK_INT <= 27) {
            canvas.drawPath(path, paint);
        } else {
            srcPath.addRect(srcRectF, Path.Direction.CCW);
            // 计算tempPath和path的差集
            srcPath.op(path, Path.Op.DIFFERENCE);
            canvas.drawPath(srcPath, paint);
        }
        // 清除Xfermode
        paint.setXfermode(null);

        // 恢复画布状态
        canvas.restore();

        drawBorders(canvas);
    }

    private void drawBorders(Canvas canvas) {
        if (isCircle) {
//            if (borderWidth > 0) {
//                drawCircleBorder(canvas, borderWidth, borderColor, radius - borderWidth / 2.0f);
//            }
//            if (innerBorderWidth > 0) {
//                drawCircleBorder(canvas, innerBorderWidth, innerBorderColor, radius - borderWidth - innerBorderWidth / 2.0f);
//            }
        } else {
            if (borderWidth > 0) {
                drawRectFBorder(canvas, dp2px(borderWidth), 0xFFEEEEEE, srcRectF, srcRadii);
            }
        }
    }

    public void drawRectFBorder(Canvas canvas, int borderWidth, int borderColor, RectF rectF, float[] radii){
        initBorderPaint(borderWidth, borderColor);
        path.addRoundRect(rectF, radii, Path.Direction.CCW);
        canvas.drawPath(path, paint);
    }

    private void initBorderPaint(int borderWidth, int borderColor) {
        path.reset();
        // 设置画笔为描边模式
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        // 描边宽度
        paint.setStrokeWidth(borderWidth);
        // 描边颜色
        paint.setColor(borderColor);
    }


    public int dp2px(float dpVal) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics()));
    }
}
